package com.jietuo.class01;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2024/3/1
 * @description: 6，在一个有序数组中，找<=某个数最右侧的位置
 */
public class Code06_BSNearRight {

    public static void main(String[] args) {
        int target = 9;
        int[] arr = new int[]{1,2,5,8,10,13,15};
        int position = getPosition(arr, target);
        System.out.println(position);
    }

    public static int getPosition(int[] arr,int target){
        int left = 0;
        int right = arr.length -1;
        int position = -1;
        while(left <= right){
            int mid = (left + right) /2;
            if (arr[mid] <= target){
                left = mid + 1;
                position = mid;
            }else{
                right = mid -1;
            }
        }
        return position;
    }
}
